<?php
	class Controller_User extends Controller_Abstract
	{
	
		function actionSearch()
		{
			if(isset($this->_context->field)){
				$field = $this->_context->field;
				$page = $this->_context->get('page', 1);
				$condition = $this->_context->condition;
				$usermodel = new User();
				$users = $usermodel->search($field, $condition, $page);

				
				$total = $usermodel->getCount();
				$pagination = new Pagination($total, $this->_context->requestUri(), $page);
				$this->_view['users'] = $users;
				$this->_view['pagination'] = $pagination->render();
				$this->_view['field'] = $field;
				$this->_view['condition'] = $condition;
			}
		}
		
		function actionAdvancedsearch(){
			$this->_view['sites'] = DataUtil::getOptionsForSites();
			
			$page = $this->_context->get('page', '1');
	       	$state = $this->_context->get('state', null);
	       	$gender = $this->_context->get('gender', null);
	       	$site = $this->_context->get('site', null);
	       	$subscribe = $this->_context->get('subscribe', null);
	       	$creditFrom = $this->_context->get('credit_from', null);
	       	$creditTo = $this->_context->get('credit_to', null);
	       	$wonFrom = $this->_context->get('won_from', null);
	       	$wonTo = $this->_context->get('won_to', null);
	       	$balanceFrom = $this->_context->get('balance_from', null);
	       	$balanceTo = $this->_context->get('balance_to', null);
	       	$createdFrom = $this->_context->get('created_from', null);
	       	$createdTo = $this->_context->get('created_to', null);
	       	$lvisitFrom = $this->_context->get('lvisit_from', null);
	       	$lvisitTo = $this->_context->get('lvisit_to', null);
	       	
	       	$usermodel = new User();
			$users = $usermodel->advancedSearch($state, $gender, $site, $creditFrom, $creditTo, $wonFrom
						, $wonTo, $balanceFrom, $balanceTo, $createdFrom, $createdTo, $lvisitFrom, $lvisitTo, $subscribe, $page);
	       	$total = $usermodel->getCount();
	       	$pagination = new Pagination($total, $this->_context->requestUri(), $page);
			
	       	$this->_view['users'] = $users;
	       	$this->_view['state'] = $state;
	       	$this->_view['gender'] = $gender;
	       	$this->_view['site'] = $site;
	       	$this->_view['subscribe'] = $subscribe;
	       	$this->_view['credit_from'] = $creditFrom;
	       	$this->_view['credit_to'] = $creditTo;
	       	$this->_view['won_from'] = $wonFrom;
	       	$this->_view['won_to'] = $wonTo;
	       	$this->_view['balance_from'] = $balanceFrom;
	       	$this->_view['balance_to'] = $balanceTo;
	       	$this->_view['created_from'] = $createdFrom;
	       	$this->_view['created_to'] = $createdTo;
	       	$this->_view['lvisit_from'] = $lvisitFrom;
	       	$this->_view['lvisit_to'] = $lvisitTo;
	       	$this->_view['pagination'] = $pagination->render();
	       	
		}	
		
		function actionSetunsub(){
			if(isset($_POST)){
				$usermodel = new User();
				
				$userid = $this->_context->post('id');
				$unsub = ($this->_context->post('unsub')==0)? '1' : '0';
				$usermodel->reverseUnsub($userid, $unsub);
				
				$field = $this->_context->post('field');
				$condition = $this->_context->post('condition');
			}
			//return $this->_redirect(url('user/search', array('field' => $field, 'condition' => $condition)));
			return $unsub;
		}
		
		function actionSetpassword(){
			if(isset($_POST)){
				$usermodel =  new User();
				
				$userid = $this->_context->post('id');
				$newpassword = $this->_context->post('password');
				if(strlen($newpassword)<6){
					return false;
				}
				$usermodel->setPassword($userid, Util::encodePassword($newpassword));
				
				$field = $this->_context->post('field');
				$condition = $this->_context->post('condition');
			}
			//return $this->_redirect(url('user/search', array('field' => $field, 'condition' => $condition)));
			return 'OK';
		}
			
		function actionRewardcredits(){
			if(isset($_POST)){
				$userid = $this->_context->post('uid', null);
				$credits = $this->_context->post('credits', null);
				
				$userModel =  new User();
				if($userModel->rewardCredits($userid, $credits)){
					return 'OK';
				}
				
			}
			//return $this->_redirect(url('user/search', array('field' => $field, 'condition' => $condition)));
			
		}
		
		function actionsendConfirmEmail()
		{
			$uid = $this->_context->post('uid', null);
			if($uid){
				$userModel = new User();
				$userInfo = $userModel->getUserByField('id', $uid);
				$siteModel = new Site();
				$siteInfo = $siteModel->getSiteById($userInfo['site_id']);
				$token = $userModel->updateToken($userInfo['id']);
				if($token){
					$activeUrl = 'http://www.'. $siteInfo['domain'] . url('user/active', array('token'=>$token, 'name'=>md5($userInfo['firstname'])));
					if(!Util::addNewEmail('register', $siteInfo['template'], array('email'=>$userInfo['email'], 'firstname'=>$userInfo['firstname'], 
									  'password' => '(Hidden)', 'domain'=>$siteInfo['domain'], 'name' => $siteInfo['name'], 'activeUrl'=>$activeUrl, 'uid'=>$userInfo['id'], 'priority'=>50))){
						return 'error';
				  	}
				  	return 'OK';
				}
			}
		}
		
		function actionGetuserinfo(){
			$uid = $this->_context->get('uid', 0);
			
			$fmodel = new Fulfillment();

			$userinfo = $fmodel->getUserInfo($uid);
			$userinfo[0]['last_visit'] = date('m/d/Y H:i:s', $userinfo[0]['last_visit']);
			$userinfo[0]['created'] = date('m/d/Y H:i:s', $userinfo[0]['created']);
			
			$user_rewards = $fmodel->getRewardsInfo($uid);
			foreach($user_rewards as $k=>$v){
				$user_rewards[$k]['rtime'] = date('m/d/Y', $user_rewards[$k]['rtime']);
			}
					
			return json_encode(array("userinfo"=>$userinfo, "user_rewards"=>$user_rewards));
			
		}
		
		function actionGettokeninfo(){
			$usermodel =  new User();
			
			$this->_view['cashdtokeninfo'] = $usermodel->getSitetokeninfo('1');
			$this->_view['lalatokeninfo'] = $usermodel->getSitetokeninfo('2');
			
			$this->_view['cashddailytokeninfo'] = $usermodel->getSitedailytokeninfo('1');
			$this->_view['laladailytokeninfo'] = $usermodel->getSitedailytokeninfo('2');
		}
	}


